package com.onlineMIS.filter;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.DateUtils;
import com.onlineMIS.common.loggerLocal;


public class SystemParm {

	public static final Properties SystemParm = new Properties();
	
	public static void load() {
		try {
			InputStream is = SystemParm.class.getClassLoader()
					 .getResourceAsStream("sysParms/SystemParm.properties");
			
			SystemParm.load(is);
			
			
		} catch (IOException e) {
			loggerLocal.error(e);
		}
	}
	
	public static String getParm(String parmName){
		String value = SystemParm.getProperty(parmName);
		if (value == null)
			value = "";
		return value.trim();
	}

	public static String getQXCategory(){
		String value = SystemParm.getProperty("QX_PURCHASE_CATEGORY_ID");
		if (StringUtils.isEmpty(value))
			return "()";
		return value;
	}
	
	
	public static Integer getQxCategoryItemValue() {
		String value = SystemParm.getProperty("QX_PURCHASE_CATEGORY_ID");
		if (StringUtils.isNotEmpty(value)) {
			String[] itme = value.split(",");
			return Integer.valueOf(itme[0].substring(1,itme[0].length()));			
		}
		return Common_util.ALL_RECORD_NEW;
	}
	
	public static Integer getXlcCategoryItemValue() {
		String value = SystemParm.getProperty("QX_PURCHASE_CATEGORY_ID");
		if (StringUtils.isNotEmpty(value)) {
			String[] itme = value.split(",");
			return Integer.valueOf(itme[1].substring(0,itme[1].length()-1));			
		}
		return Common_util.ALL_RECORD_NEW;
	}

	public static int getTestSupplierId(){
		String TEST_ID_OBJ = getParm("TEST_SUPPLIER_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;

	}
	

	public static String getQxPicURLHead() {
		return getParm("QX_PIC_URL");
	}
	
	public static String getQxPicSuffix() {
		return getParm("QX_PIC_SUFFIX");
	}

	public static int getTestClientId(){
		String TEST_ID_OBJ = getParm("TEST_CLIENT_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;
	}
	
	public static int getTestChainId(){
		String TEST_ID_OBJ = getParm("TEST_CHAIN_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;
	}
	
	public static String getPYSCategoryId(){
		String TEST_ID_OBJ = getParm("PYS_CATEGORY");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
//			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID_OBJ;
		} else 
			return "(-1)";
	}
	
	public static String getAutoLoginMatchCode() {
		return getParm("AUTO_LOGIN_MATCH_CODE");
	}
	
	public static List<Integer> getCanHideOrderRoleIds(){
		String idString = getParm("CAN_HIDE_ROLE_ID");
		if (StringUtils.isEmpty(idString)){
//			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return new ArrayList<Integer>();
		} else {
			List<Integer> list = new ArrayList<Integer>();
			String[] valueArray = idString.split(",");
			for (String value2 : valueArray){
				if (StringUtils.isBlank(value2))
					continue;
				list.add(Integer.parseInt(value2));
			}
			return list;
		}
	}
	public static Set<Integer> getParmSet(String parmName){
		Set<Integer> parmSet = new HashSet<Integer>();
		String value = getParm(parmName);
		if (!value.equals("")){
			String[] valueArray = value.split(",");
			for (String value2 : valueArray){
				if (StringUtils.isBlank(value2))
					continue;
				parmSet.add(Integer.parseInt(value2));
			}
		}
			
		return parmSet;
	}

	

	/**
	 * 获取折扣品牌
	 * @return
	 */
	public static Double getSpecialBrand(Integer brandId) {
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		
		String specialBrand = getParm("SPECIAL_BRAND_ID");
		if (specialBrand != null && specialBrand != "") {
			String brand[] = specialBrand.split(",");
			for (String brandInfo : brand) {
				if (StringUtils.isBlank(brandInfo))
					continue;
				Integer brandInfoId = Integer.valueOf(brandInfo.substring(0,brandInfo.indexOf(":")));
				double discount = Double.valueOf(brandInfo.substring(brandInfo.indexOf(":")+1,brandInfo.length()));
				map.put(brandInfoId,discount);
			}
		}
		
		if (map.get(brandId) != null) {
			return map.get(brandId);
		}else {
			return 0d;
		}				
		
	}
	
	/**
	 * 获取清账的时间(不包含预存金账户）
	 * @return
	 * @throws ParseException
	 */
	public static Date getAccountFlowDate() throws ParseException {
		String time = getParm("QX_ACCOUNT_FLOW_LATEST_DATE");
		Date date = new Date();
		if (StringUtils.isNotBlank(time)) {
			 date = Common_util.dateFormat.parse(time);
		}
		return date;
	}
	
	/**
	 * 获取清账的时间(字符串类型） 
	 * @return
	 * @throws ParseException
	 */
	public static String getAccountFlowDateS() throws ParseException {
		return getParm("QX_ACCOUNT_FLOW_LATEST_DATE");
	}
	
	
	/**
	 * 获取清账的时间(Date类型） - 预存金账户
	 * @return
	 * @throws ParseException
	 */
	public static Date getPrepayFlowDate() throws ParseException {
		String time = getParm("QX_PREPAY_FLOW_LATEST_DATE");
		Date date = new Date();
		if (StringUtils.isNotBlank(time)) {
			 date = Common_util.dateFormat.parse(time);
		}
		return date;
	}
	
	/**
	 * 获取清账的时间(字符串类型） - 预存金账户
	 * @return
	 * @throws ParseException
	 */
	public static String getPrepayFlowDateS() {
		return getParm("QX_PREPAY_FLOW_LATEST_DATE");
	}
	
	
	
	/**
	 * 获取清账的时间(不包含预存金账户）
	 * @return
	 * @throws ParseException
	 */
	public static Date getXlcAccountFlowDate() throws ParseException {
		String time = getParm("XLC_ACCOUNT_FLOW_LATEST_DATE");
		Date date = new Date();
		if (StringUtils.isNotBlank(time)) {
			 date = Common_util.dateFormat.parse(time);
		}
		return date;
	}
	
	/**
	 * 获取清账的时间(字符串类型） 
	 * @return
	 * @throws ParseException
	 */
	public static String getXlcAccountFlowDateS() throws ParseException {
		return getParm("XLC_ACCOUNT_FLOW_LATEST_DATE");
	}
	
	
	/**
	 * 获取清账的时间(Date类型） - 预存金账户
	 * @return
	 * @throws ParseException
	 */
	public static Date getXlcPrepayFlowDate() throws ParseException {
		String time = getParm("XLC_ACCOUNT_FLOW_LATEST_DATE");
		Date date = new Date();
		if (StringUtils.isNotBlank(time)) {
			 date = Common_util.dateFormat.parse(time);
		}
		return date;
	}
	
	/**
	 * 获取清账的时间(字符串类型） - 预存金账户
	 * @return
	 * @throws ParseException
	 */
	public static String getXlcPrepayFlowDateS() throws ParseException {
		return getParm("XLC_PREPAY_FLOW_LATEST_DATE");
	}
	
	/**
	 * 获取千禧客户标签id
	 * @return
	 * @throws ParseException
	 */
	public static Integer getQxCustTagId() {
		return Integer.valueOf(getParm("QX_CUST_TAG_ID"));
	}
	
	/**
	 * 获取禧乐仓客户标签id
	 * @return
	 * @throws ParseException
	 */
	public static Integer getXlcCustTagId(){
		return Integer.valueOf(getParm("XLC_CUST_TAG_ID"));
	}
	
	/**
	 * 获取连锁店可以不打折扣的最低售价
	 * @return
	 */
	public static Integer getChainLowestSellingPrice(){
		return Integer.valueOf(getParm("QX_CHAIN_LOWEST_SELLING_PRICE"));
	}
	
	
	
	
	/**
	 * 获取千禧客户表中的：盘点机信息
	 * @return
	 * @throws ParseException
	 */
	public static Integer getQxCustStocktaking() {
		return Integer.valueOf(getParm("QX_HEADQ_STOCKTAKING"));
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		getQxCategoryItemValue();

	}


}
